﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;

public partial class ThietLapPhieuChiTienMat : System.Web.UI.Page
{
    static DataTable dtPrint;
    int pc;
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmThietLapPhieuChiTienMat";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 3;
    static int CurrentPage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;
                    btnCancel_Click(sender, e);

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {

                        //drnam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThang.Items.Insert(0, new ListItem("Theo thời gian", ""));
                    dlThang.SelectedIndex = 0;
                    dlNam.SelectedValue = year2.ToString();

                    txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dlNam.Visible = false;

                    Title = "Thiết lập phiếu chi tiền mặt | " + Header;

                    btnCancel_Click(sender, e);
                    LoadNhanVien();
                    LoadTK();
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }

    void LoadTK()
    {
        drtaikhoan.Items.Clear();
        drtaikhoan.DataSource = ex.GetData("sp_ListTaiKhoan_KhongVAT");
        drtaikhoan.DataBind();
        drtaikhoan.Items.Insert(0, new ListItem("--Chọn tài khoản chuyển tiền--", ""));
        drtaikhoan.SelectedIndex = 0;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                Guid tlchi = Guid.NewGuid();
                p = new SqlParameter[12];
                p[0] = new SqlParameter("@IDs", tlchi);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[2].SqlDbType = SqlDbType.SmallDateTime;
                p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                p[3].SqlDbType = SqlDbType.Float;
                p[4] = new SqlParameter("@NoiDung", DBNull.Value);
                p[4].SqlDbType = SqlDbType.NVarChar;

                p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                p[5].SqlDbType = SqlDbType.SmallDateTime;
                p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                p[6].SqlDbType = SqlDbType.NVarChar;
                p[7] = new SqlParameter("@Del", 1);
                p[7].SqlDbType = SqlDbType.Int;
                if (drtt.SelectedIndex == 0)
                {
                    p[8] = new SqlParameter("@IDThanhToan", DBNull.Value);
                    p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                }
                else
                {
                    p[8] = new SqlParameter("@IDThanhToan", ex.GetGuid(drtt.SelectedValue));
                    p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                }
                p[9] = new SqlParameter("@UngLuong", ckungluong.Checked ? 1 : 2);
                p[9].SqlDbType = SqlDbType.Int;
                p[10] = new SqlParameter("@TinhChoChiHa", cktinhchochiha.Checked ? 1 : 2);
                p[10].SqlDbType = SqlDbType.Int;
                p[11] = new SqlParameter("@NopNganHang", ckthunganhang.Checked ? 1 : 2);
                p[11].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_xdaThietLapPhieuChiTienMat_Add", p);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[0].SqlDbType = SqlDbType.SmallDateTime;
                dt = ex.GetData("sp_xdagetsophieuchiconnotravat_TienMat",p);
                int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                if (sopc == 0)
                    pc = 1;
                else pc = sopc + 1;
                p = new SqlParameter[11];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@NoiDung", DBNull.Value);
                p[2].SqlDbType = SqlDbType.NVarChar;
                p[3] = new SqlParameter("@DonVi", DBNull.Value);
                p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                p[4] = new SqlParameter("@SoPC", pc);
                p[4].SqlDbType = SqlDbType.Int;
                p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                p[6] = new SqlParameter("@Del", 1);
                p[6].SqlDbType = SqlDbType.Int;
                p[7] = new SqlParameter("@LoaiCongNo", 2);
                p[7].SqlDbType = SqlDbType.Int;
                p[8] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                p[8].SqlDbType = SqlDbType.Float;
                p[9] = new SqlParameter("@IDLenhChi", tlchi);
                p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Add", p);
                for (int i = 0; i < GV1.Rows.Count; i++)
                {
                    TextBox txtnoidung1 = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtnoidung");
                    p = new SqlParameter[4];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NoiDung", txtnoidung1.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    p[2] = new SqlParameter("@IDPhieuChi", tlchi);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@Del", 1);
                    p[3].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdanoidungpc_Add", p);
                }
                if (ckungluong.Checked == true)
                {
                    Guid idthanhtoan = Guid.NewGuid();
                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[0].SqlDbType = SqlDbType.SmallDateTime;
                    dt = ex.GetData("sp_xdagetsophieuThanhToan_TienMat", p);
                    int sopt = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                    if (sopt == 0)
                        sopt = 1;
                    else sopt = sopt + 1;
                    p = new SqlParameter[7];
                    p[0] = new SqlParameter("@IDs", idthanhtoan);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    p[3] = new SqlParameter("@IDPhieuChi", tlchi);
                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[4] = new SqlParameter("@TonTheoPhieuChi", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[4].SqlDbType = SqlDbType.Float;
                    p[5] = new SqlParameter("@Del", 1);
                    p[5].SqlDbType = SqlDbType.Int;
                    p[6] = new SqlParameter("@SoPhieuTT", sopt);
                    p[6].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdathanhtoantienmat_Add", p);

                    p = new SqlParameter[20];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NoiDung", "Thanh toán tiền lương cho" + drnhanvien.SelectedItem.Text);
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    p[2] = new SqlParameter("@DoiTuong", DBNull.Value);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@DonVi", DBNull.Value);
                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[4] = new SqlParameter("@HangMuc", DBNull.Value);
                    p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[5] = new SqlParameter("@SoLuong", DBNull.Value);
                    p[5].SqlDbType = SqlDbType.Float;
                    p[6] = new SqlParameter("@DonGia", DBNull.Value);
                    p[6].SqlDbType = SqlDbType.Float;
                    p[7] = new SqlParameter("@TienThanhToan", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[7].SqlDbType = SqlDbType.Float;
                    p[8] = new SqlParameter("@CoVAT", DBNull.Value);
                    p[8].SqlDbType = SqlDbType.Int;
                    p[9] = new SqlParameter("@CoHoaDon", DBNull.Value);
                    p[9].SqlDbType = SqlDbType.Int;
                    p[10] = new SqlParameter("@IDThanhToan", idthanhtoan);
                    p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[11] = new SqlParameter("@Del", 1);
                    p[11].SqlDbType = SqlDbType.Int;
                    p[12] = new SqlParameter("@NhaCungCap", DBNull.Value);
                    p[12].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[13] = new SqlParameter("@NhomVatLieu", DBNull.Value);
                    p[13].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[14] = new SqlParameter("@LoaiVatLieu", DBNull.Value);
                    p[14].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[15] = new SqlParameter("@MaThietBi", DBNull.Value);
                    p[15].SqlDbType = SqlDbType.NVarChar;
                    p[16] = new SqlParameter("@TinhChoChiHa", DBNull.Value);
                    p[16].SqlDbType = SqlDbType.NVarChar;
                    p[17] = new SqlParameter("@TaiSanCoDinh", DBNull.Value);
                    p[17].SqlDbType = SqlDbType.NVarChar;
                    p[18] = new SqlParameter("@NhapKho", DBNull.Value);
                    p[18].SqlDbType = SqlDbType.Int;
                    p[19] = new SqlParameter("@ThanhTienHang", DBNull.Value);
                    p[19].SqlDbType = SqlDbType.Float;
                    ex.ExecuteNonQuerry("sp_XDANoiDungTTTienMat_Add", p);


                    //Guid thu = Guid.NewGuid();
                    //p = new SqlParameter[9];
                    //p[0] = new SqlParameter("@IDs", thu);
                    //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                    //p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    //p[2].SqlDbType = SqlDbType.SmallDateTime;
                    //p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    //p[3].SqlDbType = SqlDbType.Float;
                    //p[4] = new SqlParameter("@NoiDung", "Thanh toán ứng lương:" + drnhanvien.SelectedItem.Text + txtngaythang.Text.Trim());
                    //p[4].SqlDbType = SqlDbType.NVarChar;

                    //p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                    //p[5].SqlDbType = SqlDbType.SmallDateTime;
                    //p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                    //p[6].SqlDbType = SqlDbType.NVarChar;
                    //p[7] = new SqlParameter("@Del", 1);
                    //p[7].SqlDbType = SqlDbType.Int;
                    //p[8] = new SqlParameter("@IDThanhToan", idthanhtoan);
                    //p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                    //ex.ExecuteNonQuerry("sp_XDAThietLapPhieuThuTienMat_Add", p);


                    //dt = ex.GetData("sp_xdagetsophieuthuconnotravat_TienMat");
                    //int sopt1 = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                    //if (sopt1 == 0)
                    //    sopt1 = 1;
                    //else sopt1 = sopt1 + 1;
                    //p = new SqlParameter[11];
                    //p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    //p[1].SqlDbType = SqlDbType.SmallDateTime;
                    //p[2] = new SqlParameter("@NoiDung", "Thanh toán ứng lương:" + drnhanvien.SelectedItem.Text + txtngaythang.Text.Trim());
                    //p[2].SqlDbType = SqlDbType.NVarChar;
                    //p[3] = new SqlParameter("@DonVi", DBNull.Value);
                    //p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[4] = new SqlParameter("@SoPC", sopt1);
                    //p[4].SqlDbType = SqlDbType.Int;
                    //p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                    //p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[6] = new SqlParameter("@Del", 1);
                    //p[6].SqlDbType = SqlDbType.Int;
                    //p[7] = new SqlParameter("@LoaiCongNo", 2);
                    //p[7].SqlDbType = SqlDbType.Int;
                    //p[8] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    //p[8].SqlDbType = SqlDbType.Float;
                    //p[9] = new SqlParameter("@IDLenhChi", thu);
                    //p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                    //p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                    //ex.ExecuteNonQuerry("sp_XDAPhieuChiCongNoThu_Add", p);


                    p = new SqlParameter[5];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@IDThanhToan", idthanhtoan);
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@IDPhieuChi", tlchi);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@SoTienThanhToan", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.Float;
                    p[4] = new SqlParameter("@Del", 1);
                    p[4].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                    
                }

                int sopc1;
                if (ckthunganhang.Checked == true)
                {

                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[0].SqlDbType = SqlDbType.SmallDateTime;
                    dt = ex.GetData("sp_xdagetsophieuchiconnothuvat_NganHang", p);
                    sopc1 = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                    if (sopc1 == 0)
                    {
                        pc = 1;

                    }
                    else pc = sopc1 + 1;
                    p = new SqlParameter[11];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.SmallDateTime;
                    p[2] = new SqlParameter("@NoiDung", "Nộp ngân hàng");
                    p[2].SqlDbType = SqlDbType.NVarChar;
                    p[3] = new SqlParameter("@DonVi", DBNull.Value);
                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[4] = new SqlParameter("@SoPC", pc);
                    p[4].SqlDbType = SqlDbType.Int;

                    p[5] = new SqlParameter("@TaiKhoan", ex.GetGuid(drtaikhoan.SelectedValue));
                    p[5].SqlDbType = SqlDbType.UniqueIdentifier;


                    p[6] = new SqlParameter("@Del", 1);
                    p[6].SqlDbType = SqlDbType.Int;
                    p[7] = new SqlParameter("@LoaiCongNo", 1);
                    p[7].SqlDbType = SqlDbType.Int;
                    p[8] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[8].SqlDbType = SqlDbType.Float;
                    p[9] = new SqlParameter("@IDLenhChi", tlchi);
                    p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                    p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_XDAPhieuChiCongNoThu_Add", p);


                }
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập phiếu chi tiền mặt\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtbophan.Text = "";
        txtchucvu.Text = "";
        txtsotien.Text = "";
        txtngaythang.Text = "";
        FirstGridViewRow();
        txtbophan.Enabled = false;
        txtchucvu.Enabled = false;
        drnhanvien.SelectedIndex = 0;
        LoadNhanVienTK();
        //p = new SqlParameter[2];
        //p[0] = new SqlParameter("@PageNumber", 1);
        //p[0].SqlDbType = SqlDbType.Int;
        //p[1] = new SqlParameter("@RowspPage", PageNumber);
        //p[1].SqlDbType = SqlDbType.Int;
        ListDropNhanVien();
        //GV.DataSource = ex.GetData("sp_xdathietlapphieuchitienmat_List", p);
        //GV.DataBind();
        GVPhieuChi.DataSource = ex.GetData("sp_PhieuChiCongNo_TienMat");
        GVPhieuChi.DataBind();
        LoadFooterGV();
        dt = ex.GetData("sp_LoadFooter_TLChiTienMat");

        LoadFooter();
        btnSave.Visible = true;
        btncapnhat.Visible = false;
        LoadTT();
        LoadNCCTK();
        ckungluong.Checked = false;
        cktinhchochiha.Checked = false;
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {


        if (e.CommandName.Equals("Select"))
        {
            string ID = e.CommandArgument.ToString();
            hdID.Value = ID;
            if (ID.Equals("")) { gstGetMess("Dữ liệu không sửa được", ""); }
            else
            {
                Guid gID = new Guid(ID);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                DataTable dt = ex.GetData("sp_xdaThietLapPhieuChiTienMat_ListByID", p);
                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        drnhanvien.SelectedValue = dt.Rows[0]["nhanvien"].ToString();
                        txtbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                        txtchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
                        txtsotien.Text = dt.Rows[0]["sotien"].ToString();
                        txtngaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                        //ckungluong.Checked = dt.Rows[0]["ungluong"].ToString().Equals(1) ? true : false;

                        if (dt.Rows[0]["ungluong"].ToString() == "1")
                            ckungluong.Checked = true;
                        else
                            ckungluong.Checked = false;
                    }


                    catch (Exception ax)
                    {
                        gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập phiếu chi tiền mặt\t" + ax.Message);
                    }

                }

                int rowIndex = 0;
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                dt = ex.GetData("sp_xdaThietLapPhieuChiTienMat_ListByID", p);
                if (ViewState["CurrentTable"] != null)
                {

                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");

                            GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                            txtnoidung.Focus();
                            txtnoidung.Text = dt.Rows[i]["noidung"].ToString();

                            rowIndex++;
                            if (txtnoidung.Text != null)
                            {
                                AddNewRow();
                            }
                        }
                    }

                }

                else
                {
                    Response.Write("ViewState is null");
                }



                btncapnhat.Visible = true;
                btnSave.Visible = false;
            }
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                string ID = e.CommandArgument.ToString();
                hdID.Value = ID;
                if (ID.Equals("")) { gstGetMess("Dữ liệu không thể xóa", ""); }
                else
                {
                    if (f_Xoa.Equals(1))
                    {
                        Guid gID = new Guid(ID);
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        try
                        {

                            string ID3 = e.CommandArgument.ToString();
                            hdID.Value = ID3;
                            Guid gID3 = new Guid(ID3);
                            p = new SqlParameter[1];
                            p[0] = new SqlParameter("@IDs", gID3);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_xdaphieuchicongno_Del", p);
                            p = new SqlParameter[1];
                            p[0] = new SqlParameter("@IDs", gID);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_xdanoidungpc_Del", p);

                            p = new SqlParameter[1];
                            p[0] = new SqlParameter("@IDs", gID);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_xdathietlapphieuchitienmat_DelThuNganHang", p);

                            p[0] = new SqlParameter("@IDs", gID);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_TLThanhToan_DelCongNoTHuTienMat_Luong", p);
                            p[0] = new SqlParameter("@IDs", gID);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_TLThanhToan_DelThietLapPTHuTienMat_Luong", p);
                            p[0] = new SqlParameter("@IDs", gID);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("Del_XDANoiDungTTTienMat_UngLuong", p);
                            p[0] = new SqlParameter("@IDs", gID);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_TLThanhToan_DelThanhToan_UngLuong", p);
                            p[0] = new SqlParameter("@IDs", gID);
                            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                            ex.ExecuteNonQuerry("sp_xdaThietLapPhieuChiTienMat_Del", p);
                            btnCancel_Click(sender, e);
                            gstGetMess("Xóa thành công", "");
                        }
                        catch (Exception ax)
                        {
                            gstGetMess("Có lỗi trong quá trình xóa, xem trong file log", "");
                            ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập phiếu chi tiền mặt\t" + ax.Message);

                        }
                    }
                    else
                        gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
                }
            }
            else if (e.CommandName.Equals("chitietphieuthu"))
            {
                string ID = e.CommandArgument.ToString();
                hdID.Value = ID;
                Guid gID = new Guid(ID);
                p = new SqlParameter[1];

                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                dt = ex.GetData("sp_xdaThietLapPhieuChi_ChiTietPhieuThu", p);
                if (dt.Rows.Count > 0)
                {
                    thungaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                    thuhoten.Text = dt.Rows[0]["hoten"].ToString();
                    thubophan.Text = dt.Rows[0]["tenbophan"].ToString();
                    thuchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();

                    thusophieu.Text = dt.Rows[0]["sopc"].ToString();
                    thusopc.Text = dt.Rows[0]["sophieuchi"].ToString();
                    thusott.Text = dt.Rows[0]["SoPhieuTT"].ToString();
                    thuvietbangchu.Text = "(" + ToString(decimal.Parse(dt.Rows[0]["sotien"].ToString())) + ")";
                }
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                GridView3.DataSource = ex.GetData("sp_xdaThietLapThu_ChiTietPhieuThu_NoiDung", p);
                GridView3.DataBind();
                GridView3.FooterRow.Cells[0].Text = "Tổng cộng";
                GridView3.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;

                GridView3.FooterRow.Cells[2].Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotien"].ToString()));
                GridView3.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Right;
                ModalPopupExtender1.Show();
            }
            else if (e.CommandName.Equals("chitietthanhtoan"))
            {
                string ID = e.CommandArgument.ToString();
                hdID.Value = ID;
                Guid gID = new Guid(ID);
                p = new SqlParameter[1];

                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                dt = ex.GetData("[sp_xdaChiTietThanhToan_ThôngTinNV]", p);
                if (dt.Rows.Count > 0)
                {
                    ttngaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                    tthoten.Text = dt.Rows[0]["hoten"].ToString();
                    ttbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                    ttchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
                    ttsophieu.Text = dt.Rows[0]["sophieutt"].ToString();
                    lbphieuthu.Text = dt.Rows[0]["sopt"].ToString();
                    
                }
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                GVDoiTuong.DataSource = ex.GetData("sp_ChiTietThanhToan_List_NoiDung", p);
                GVDoiTuong.DataBind();
                GVDoiTuong.FooterRow.Cells[0].Text = "Tổng cộng";
                GVDoiTuong.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;
                double s1 = 0;
                for (int i = 0; i < GVDoiTuong.Rows.Count; i++)
                {
                    s1 += double.Parse(GVDoiTuong.Rows[i].Cells[6].Text.Trim().Replace(",", ""));

                }
                GVDoiTuong.FooterRow.Cells[6].Text = s1 == 0 ? "0" : string.Format("{0:0,0}", s1);
                GVDoiTuong.FooterRow.Cells[6].HorizontalAlign = HorizontalAlign.Right;
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                dt = ex.GetData("sp_ChiTietThanhToan", p);
                ltlFirst.Text = "";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ltlFirst.Text += dt.Rows[i]["sopc"].ToString() + ",";
                }
                ltlFirst.Text = ltlFirst.Text.Substring(0, ltlFirst.Text.Length - 1);
                chitietthanhtoan.Show();
            }
            else if (e.CommandName.Equals("chitiet"))
            {
                string ID = e.CommandArgument.ToString();
                hdID.Value = ID;
                Guid gID = new Guid(ID);
                p = new SqlParameter[1];

                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                dt = ex.GetData("sp_xdaThietLapPhieuChi_ChiTietPhieuChi", p);
                if (dt.Rows.Count > 0)
                {
                    pcngaythang.Text ="  "+ dt.Rows[0]["ngaythang"].ToString();
                    pchoten.Text = dt.Rows[0]["hoten"].ToString();
                    pcbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                    pcchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
                    pctt.Text = dt.Rows[0]["sophieutt"].ToString();
                    pcpt.Text = dt.Rows[0]["sophieuthu"].ToString();
                    pcsophieuchi.Text = dt.Rows[0]["sopc"].ToString();
                    lbvietbangchu.Text = "("+ToString(decimal.Parse(dt.Rows[0]["sotien"].ToString()))+")";
                }
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                GridView1.DataSource = ex.GetData("sp_xdaThietLapPhieuChi_ChiTietPhieuChi_NoiDung", p);
                GridView1.DataBind();
                GridView1.FooterRow.Cells[0].Text = "Tổng cộng";
                GridView1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Center;

                GridView1.FooterRow.Cells[2].Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotien"].ToString()));
                GridView1.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Right;
                md.Show();
            }
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            try
            {
                Guid gID = new Guid(hdID.Value);


                p = new SqlParameter[3];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                p[2].SqlDbType = SqlDbType.Float;
                ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpDate_PCCongNo", p);

                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_Del_MoiDungPC", p);
                for (int i = 0; i < GV1.Rows.Count; i++)
                {
                    TextBox txtnoidung1 = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtnoidung");
                    if (txtnoidung1.Text != "")
                    {

                        p = new SqlParameter[4];
                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[1] = new SqlParameter("@NoiDung", txtnoidung1.Text.Trim());
                        p[1].SqlDbType = SqlDbType.NVarChar;
                        p[2] = new SqlParameter("@IDPhieuChi", gID);
                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[3] = new SqlParameter("@Del", 1);
                        p[3].SqlDbType = SqlDbType.Int;
                        if (txtnoidung1.Text != "")
                            ex.ExecuteNonQuerry("sp_xdanoidungpc_Add", p);
                    }
                }
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                dt = ex.GetData("sp_ThietLapPhieuChiTienMat_TrangThaiUngLUong", p);
                int ung = Int16.Parse(dt.Rows[0]["ungluong"].ToString());

                if (ckungluong.Checked == true && ung == 2)
                {
                    Guid idthanhtoan = Guid.NewGuid();
                    p = new SqlParameter[1];
                    p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[0].SqlDbType = SqlDbType.SmallDateTime;
                    dt = ex.GetData("sp_xdagetsophieuThanhToan_TienMat", p);
                    int sopt = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                    if (sopt == 0)
                        sopt = 1;
                    else sopt = sopt + 1;
                    p = new SqlParameter[7];
                    p[0] = new SqlParameter("@IDs", idthanhtoan);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    p[3] = new SqlParameter("@IDPhieuChi", gID);
                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[4] = new SqlParameter("@TonTheoPhieuChi", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[4].SqlDbType = SqlDbType.Float;
                    p[5] = new SqlParameter("@Del", 1);
                    p[5].SqlDbType = SqlDbType.Int;
                    p[6] = new SqlParameter("@SoPhieuTT", sopt);
                    p[6].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdathanhtoantienmat_Add", p);

                    p = new SqlParameter[20];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NoiDung", "Thanh toán tiền lương cho" + drnhanvien.SelectedItem.Text);
                    p[1].SqlDbType = SqlDbType.NVarChar;
                    p[2] = new SqlParameter("@DoiTuong", DBNull.Value);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@DonVi", DBNull.Value);
                    p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[4] = new SqlParameter("@HangMuc", DBNull.Value);
                    p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[5] = new SqlParameter("@SoLuong", DBNull.Value);
                    p[5].SqlDbType = SqlDbType.Float;
                    p[6] = new SqlParameter("@DonGia", DBNull.Value);
                    p[6].SqlDbType = SqlDbType.Float;
                    p[7] = new SqlParameter("@TienThanhToan", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[7].SqlDbType = SqlDbType.Float;
                    p[8] = new SqlParameter("@CoVAT", DBNull.Value);
                    p[8].SqlDbType = SqlDbType.Int;
                    p[9] = new SqlParameter("@CoHoaDon", DBNull.Value);
                    p[9].SqlDbType = SqlDbType.Int;
                    p[10] = new SqlParameter("@IDThanhToan", idthanhtoan);
                    p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[11] = new SqlParameter("@Del", 1);
                    p[11].SqlDbType = SqlDbType.Int;
                    p[12] = new SqlParameter("@NhaCungCap", DBNull.Value);
                    p[12].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[13] = new SqlParameter("@NhomVatLieu", DBNull.Value);
                    p[13].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[14] = new SqlParameter("@LoaiVatLieu", DBNull.Value);
                    p[14].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[15] = new SqlParameter("@MaThietBi", DBNull.Value);
                    p[15].SqlDbType = SqlDbType.NVarChar;
                    p[16] = new SqlParameter("@TinhChoChiHa", DBNull.Value);
                    p[16].SqlDbType = SqlDbType.NVarChar;
                    p[17] = new SqlParameter("@TaiSanCoDinh", DBNull.Value);
                    p[17].SqlDbType = SqlDbType.NVarChar;
                    p[18] = new SqlParameter("@NhapKho", DBNull.Value);
                    p[18].SqlDbType = SqlDbType.Int;
                    p[19] = new SqlParameter("@ThanhTienHang",DBNull.Value);
                    p[19].SqlDbType = SqlDbType.Float;
                    ex.ExecuteNonQuerry("sp_XDANoiDungTTTienMat_Add", p);

                    //Guid thu = Guid.NewGuid();
                    //p = new SqlParameter[9];
                    //p[0] = new SqlParameter("@IDs", thu);
                    //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                    //p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    //p[2].SqlDbType = SqlDbType.SmallDateTime;
                    //p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    //p[3].SqlDbType = SqlDbType.Float;
                    //p[4] = new SqlParameter("@NoiDung", "Thanh toán ứng lương:" + drnhanvien.SelectedItem.Text + txtngaythang.Text.Trim());
                    //p[4].SqlDbType = SqlDbType.NVarChar;

                    //p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                    //p[5].SqlDbType = SqlDbType.SmallDateTime;
                    //p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                    //p[6].SqlDbType = SqlDbType.NVarChar;
                    //p[7] = new SqlParameter("@Del", 1);
                    //p[7].SqlDbType = SqlDbType.Int;
                    //p[8] = new SqlParameter("@IDThanhToan", idthanhtoan);
                    //p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                    //ex.ExecuteNonQuerry("sp_XDAThietLapPhieuThuTienMat_Add", p);


                    //dt = ex.GetData("sp_xdagetsophieuthuconnotravat_TienMat");
                    //int sopt1 = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                    //if (sopt1 == 0)
                    //    sopt1 = 1;
                    //else sopt1 = sopt1 + 1;
                    //p = new SqlParameter[11];
                    //p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    //p[1].SqlDbType = SqlDbType.SmallDateTime;
                    //p[2] = new SqlParameter("@NoiDung", "Thanh toán ứng lương:" + drnhanvien.SelectedItem.Text + txtngaythang.Text.Trim());
                    //p[2].SqlDbType = SqlDbType.NVarChar;
                    //p[3] = new SqlParameter("@DonVi", DBNull.Value);
                    //p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[4] = new SqlParameter("@SoPC", sopt1);
                    //p[4].SqlDbType = SqlDbType.Int;
                    //p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                    //p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[6] = new SqlParameter("@Del", 1);
                    //p[6].SqlDbType = SqlDbType.Int;
                    //p[7] = new SqlParameter("@LoaiCongNo", 2);
                    //p[7].SqlDbType = SqlDbType.Int;
                    //p[8] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    //p[8].SqlDbType = SqlDbType.Float;
                    //p[9] = new SqlParameter("@IDLenhChi", thu);
                    //p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                    //p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                    //p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                    //ex.ExecuteNonQuerry("sp_XDAPhieuChiCongNoThu_Add", p);

                    p = new SqlParameter[5];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@IDThanhToan", idthanhtoan);
                    p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[2] = new SqlParameter("@IDPhieuChi", gID);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@SoTienThanhToan", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.Float;
                    p[4] = new SqlParameter("@Del", 1);
                    p[4].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_XDAChiTietThanhToan_Add", p);
                }
                else if (ckungluong.Checked == true && ung == 1)
                {
                    p = new SqlParameter[2];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.SmallDateTime;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpdateTT", p);

                    p = new SqlParameter[2];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.Float;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpdateNoiDungTT", p);
                    p = new SqlParameter[3];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.Float;
                    p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpdateThietlapThu", p);

                    p = new SqlParameter[3];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.Float;
                    p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpdatePhieuThuCongNo", p);

                    p = new SqlParameter[2];
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.Float;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_UpdateChiTietThanhToan", p);
                }

                else if (ckungluong.Checked == false && ung == 1)
                {


                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_TLThanhToan_DelCongNoTHuTienMat_Luong", p);
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_TLThanhToan_DelThietLapPTHuTienMat_Luong", p);
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("Del_XDANoiDungTTTienMat_UngLuong", p);
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_TLThanhToan_DelThanhToan_UngLuong", p);
                    p[0] = new SqlParameter("@IDs", gID);
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    ex.ExecuteNonQuerry("sp_ThietLapPhieuChiTienMat_DelChiTietThanhToan", p);
                }

                p = new SqlParameter[10];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[2].SqlDbType = SqlDbType.SmallDateTime;
                p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                p[3].SqlDbType = SqlDbType.Float;
                p[4] = new SqlParameter("@NoiDung", DBNull.Value);
                p[4].SqlDbType = SqlDbType.NVarChar;

                p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                p[5].SqlDbType = SqlDbType.SmallDateTime;
                p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                p[6].SqlDbType = SqlDbType.NVarChar;
                p[7] = new SqlParameter("@Del", 1);
                p[7].SqlDbType = SqlDbType.Int;
                p[8] = new SqlParameter("@UngLuong", ckungluong.Checked ? 1 : 2);
                p[8].SqlDbType = SqlDbType.Int;
                p[9] = new SqlParameter("@TinhChoChiHa", cktinhchochiha.Checked ? 1 : 2);
                p[9].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_xdaThietLapPhieuChiTienMat_Update", p);

                btnCancel_Click(sender, e);
                gstGetMess("Cập nhật thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình cập nhật, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập phiếu chi tiền mặt\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền sửa dữ liệu trên trang này", "");
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        //if (CurrentPage > 1)
        //{
        //    CurrentPage--;
        //    btnPre.Enabled = true;
        //    btnNext.Enabled = true;
        //    btnFirst.Enabled = true;

        //    p = new SqlParameter[2];
        //    p[0] = new SqlParameter("@PageNumber", CurrentPage);
        //    p[0].SqlDbType = SqlDbType.Int;
        //    p[1] = new SqlParameter("@RowspPage", PageNumber);
        //    p[1].SqlDbType = SqlDbType.Int;

        //    GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChiTienMat_List", p);
        //    GV.DataBind();

        //}
        //else
        //{
        //    btnFirst.Enabled = false;
        //    btnPre.Enabled = false;
        //    btnNext.Enabled = true;
        //}
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        //CurrentPage++;
        //p = new SqlParameter[2];
        //p[0] = new SqlParameter("@PageNumber", CurrentPage);
        //p[0].SqlDbType = SqlDbType.Int;
        //p[1] = new SqlParameter("@RowspPage", PageNumber);
        //p[1].SqlDbType = SqlDbType.Int;

        //GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChiTienMat_List", p);
        //GV.DataBind();

        //if (GV.Rows.Count.Equals(0))
        //{
        //    btnPre.Enabled = true;
        //    btnNext.Enabled = false;
        //}
        //else
        //{
        //    btnPre.Enabled = true;
        //    btnNext.Enabled = true;
        //}
        //btnFirst.Enabled = true;
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        //CurrentPage = 1;
        //p = new SqlParameter[2];
        //p[0] = new SqlParameter("@PageNumber", CurrentPage);
        //p[0].SqlDbType = SqlDbType.Int;
        //p[1] = new SqlParameter("@RowspPage", PageNumber);
        //p[1].SqlDbType = SqlDbType.Int;

        //GV.DataSource = ex.GetData("sp_xdaThietLapPhieuChiTienMat_List", p);
        //GV.DataBind();

        //btnPre.Enabled = false;
        //btnNext.Enabled = true;
        //btnFirst.Enabled = false;
    }


    void LoadNhanVien()
    {
        drnhanvien.Items.Clear();
        drnhanvien.DataSource = ex.GetData("sp_ListNhanVien_PCTienMat");
        drnhanvien.DataBind();
        drnhanvien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        drnhanvien.SelectedIndex = 0;
    }
    void LoadNhanVienTK()
    {
        //dlnhanvien.Items.Clear();
        //dlnhanvien.DataSource = ex.GetData("sp_ThietLapPhieuChiTienMAt");
        //dlnhanvien.DataBind();
        //dlnhanvien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        //dlnhanvien.SelectedIndex = 0;
    }
    void LoadNCCTK()
    {
        dropnhacungcap.Items.Clear();
        dropnhacungcap.DataSource = ex.GetData("sp_PhieuChiCongNo_NhaCungCap");
        dropnhacungcap.DataBind();
        dropnhacungcap.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", ""));
        dropnhacungcap.SelectedIndex = 0;
    }

    protected void drnhanvien_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", ex.GetGuid(drnhanvien.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        DataTable dt = ex.GetData("sp_ListBoPhan_ChucVu", p);
        if (dt.Rows.Count > 0)
        {
            try
            {
                txtbophan.Text = dt.Rows[0]["tenbophan"].ToString();
                txtchucvu.Text = dt.Rows[0]["tenchucvu"].ToString();
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tThiết lập phiếu chi tiền mặt\t" + ax.Message);
            }
        }
        ListPhieuChiTheoNhanVien();
    }
    protected void btnXem_Click(object sender, EventArgs e)
    {
        getDate();
        if (dropNhanVien.SelectedIndex == 0 && dropnhacungcap.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;

            GVPhieuChi.DataSource = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NhaCC", p);
            GVPhieuChi.DataBind();
            LoadFooterGV();
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            dtPrint = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NhaCC_Print", p);
        }
        else
            if (dropNhanVien.SelectedIndex == 0 && dropnhacungcap.SelectedIndex > 0)
            {
                p = new SqlParameter[3];
                p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
                p[0].SqlDbType = SqlDbType.SmallDateTime;
                p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
                p[1].SqlDbType = SqlDbType.SmallDateTime;


                p[2] = new SqlParameter("@NhaCungCap", dropnhacungcap.SelectedValue);
                p[2].SqlDbType = SqlDbType.NVarChar;
                GVPhieuChi.DataSource = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NCC", p);
                GVPhieuChi.DataBind();
                LoadFooterGV();
                //btnFirst.Visible = false;
                //btnPre.Visible = false;
                //btnNext.Visible = false;
            }
    }
    public void ListPhieuChiTheoNhanVien()
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drnhanvien.SelectedValue));
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;
        drtt.Items.Clear();
        drtt.DataSource = ex.GetData("sp_PhieuChiTienMat_ListThanhToan", p);
        drtt.DataBind();
        drtt.Items.Insert(0, new ListItem("--Chọn số thanh toán--", ""));
        drtt.SelectedIndex = 0;
    }

    public void ListDropNhanVien()
    {

        dropNhanVien.Items.Clear();
        dropNhanVien.DataSource = ex.GetData("sp_PhieuChiCongNo_NhanVien");
        dropNhanVien.DataBind();
        dropNhanVien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        dropNhanVien.SelectedIndex = 0;
    }

    void LoadTT()
    {
        drtt.Items.Clear();

        drtt.Items.Insert(0, new ListItem("--Chọn thanh toán--", ""));
        drtt.SelectedIndex = 0;
    }
    protected void btnxempc_Click(object sender, EventArgs e)
    {

        //if (dlnhanvien.SelectedIndex == 0)
        //{
        //    p = new SqlParameter[2];
        //    p[0] = new SqlParameter("@Thang", int.Parse(drthang.SelectedValue));
        //    p[0].SqlDbType = SqlDbType.Int;
        //    p[1] = new SqlParameter("@Nam", int.Parse(drnam.SelectedValue));
        //    p[1].SqlDbType = SqlDbType.Int;

        //    GV.DataSource = ex.GetData("sp_xdathietlapphieuchitienmat_List_ThoThoigian", p);
        //    GV.DataBind();
        //    p = new SqlParameter[2];
        //    p[0] = new SqlParameter("@Thang", int.Parse(drthang.SelectedValue));
        //    p[0].SqlDbType = SqlDbType.Int;
        //    p[1] = new SqlParameter("@Nam", int.Parse(drnam.SelectedValue));
        //    p[1].SqlDbType = SqlDbType.Int;
        //    dt = ex.GetData("sp_LoadFooter_TLChiTienMat_TheoThoiGian", p);

        //    LoadFooter();
        //    btnFirst.Visible = false;
        //    btnPre.Visible = false;
        //    btnNext.Visible = false;
        //}
        //else
        //{
        //    p = new SqlParameter[3];
        //    p[0] = new SqlParameter("@Thang", int.Parse(drthang.SelectedValue));
        //    p[0].SqlDbType = SqlDbType.Int;
        //    p[1] = new SqlParameter("@Nam", int.Parse(drnam.SelectedValue));
        //    p[1].SqlDbType = SqlDbType.Int;
        //    p[2] = new SqlParameter("@NhanVien", ex.GetGuid(dlnhanvien.SelectedValue));
        //    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        //    GV.DataSource = ex.GetData("sp_xdathietlapphieuchitienmat_List_ThoThoigian_NhanVien", p);
        //    GV.DataBind();
        //    p = new SqlParameter[3];
        //    p[0] = new SqlParameter("@Thang", int.Parse(drthang.SelectedValue));
        //    p[0].SqlDbType = SqlDbType.Int;
        //    p[1] = new SqlParameter("@Nam", int.Parse(drnam.SelectedValue));
        //    p[1].SqlDbType = SqlDbType.Int;
        //    p[2] = new SqlParameter("@NhanVien", ex.GetGuid(dlnhanvien.SelectedValue));
        //    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        //    dt = ex.GetData("sp_LoadFooter_TLChiTienMat_TheoThoiGian_NV", p);

        //    LoadFooter();
        //    btnFirst.Visible = false;
        //    btnPre.Visible = false;
        //    btnNext.Visible = false;
        //}


    }
    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (dlThang.SelectedIndex.Equals(0))
        {
            txtTuNgay.Visible = true;
            txtDenNgay.Visible = true;
            dlNam.Visible = false;
        }
        else
        {
            txtTuNgay.Visible = false;
            txtDenNgay.Visible = false;
            dlNam.Visible = true;
        }
        getDate();

        p = new SqlParameter[2];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        dropNhanVien.Items.Clear();
        dropNhanVien.DataSource = ex.GetData("sp_PhieuChiCongNo_NhanVien_TheoThoiGian", p);
        dropNhanVien.DataBind();
        dropNhanVien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        dropNhanVien.SelectedIndex = 0;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        dropnhacungcap.Items.Clear();
        dropnhacungcap.DataSource = ex.GetData("sp_PhieuChiCongNo_NhaCungCap_TheoThoiGian", p);
        dropnhacungcap.DataBind();
        dropnhacungcap.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", ""));
        dropnhacungcap.SelectedIndex = 0;
    }
    public void getDate()
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
        else
        {
            txtTuNgay.Text = "1/" + dlThang.SelectedValue + "/" + dlNam.SelectedValue;
            txtDenNgay.Text = GetLastDayOfMonth(int.Parse(dlThang.SelectedValue)).ToString("dd/MM/yyyy");
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
    }
    public DateTime GetLastDayOfMonth(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(dlNam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }

    void LoadFooterGV()
    {
        double sotien = 0;
        for (int i = 0; i < GVPhieuChi.Rows.Count; i++)
        {

            sotien += double.Parse(GVPhieuChi.Rows[i].Cells[6].Text.Trim().Replace(",", ""));


        }

        if (GVPhieuChi.Rows.Count > 0)
        {
            GVPhieuChi.FooterRow.Cells[3].Text = "Tổng số";
            GVPhieuChi.FooterRow.Cells[3].HorizontalAlign = HorizontalAlign.Center;
            //GV.FooterRow.Cells[4].HorizontalAlign = HorizontalAlign.Center;
            GVPhieuChi.FooterRow.Cells[6].Text = sotien > 999 ? string.Format("{0:0,0}", sotien) : sotien.ToString();
            GVPhieuChi.FooterRow.Cells[6].HorizontalAlign = HorizontalAlign.Right;

        }
    }
    protected void drthang_SelectedIndexChanged(object sender, EventArgs e)
    {
        //p = new SqlParameter[2];
        //p[0] = new SqlParameter("@Thang", int.Parse(drthang.SelectedValue));
        //p[0].SqlDbType = SqlDbType.Int;
        //p[1] = new SqlParameter("@Nam", int.Parse(drnam.SelectedValue));
        //p[1].SqlDbType = SqlDbType.Int;
        //dlnhanvien.Items.Clear();
        //dlnhanvien.DataSource = ex.GetData("sp_ThietLapPhieuChiTienMAt_TheoThoiGian", p);
        //dlnhanvien.DataBind();
        //dlnhanvien.Items.Insert(0, new ListItem("--Chọn nhân viên--", ""));
        //dlnhanvien.SelectedIndex = 0;
    }

    private void LoadFooter()
    {
        //string[] field = { "sotien" };
        //if (dt.Rows.Count > 0)
        //{
        //    GV.FooterRow.Cells[2].Text = "Tổng cộng";
        //    GV.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Right;

        //    GV.FooterRow.Cells[5].Text = ConvertDecimal(double.Parse(dt.Compute("sum(" + field[0] + ")", "").ToString()));
        //    GV.FooterRow.Cells[5].HorizontalAlign = HorizontalAlign.Right;
        //}
    }
    string ConvertDecimal(double s)
    {
        string q = "";
        if (s.ToString().Contains("."))
        {
            double x = double.Parse(s.ToString().Substring(0, s.ToString().IndexOf('.')));
            q = (x != 0 && x > 999) ? string.Format("{0:0,0}", x) : x.ToString();
            q += s.ToString().Substring(s.ToString().IndexOf('.'));
        }
        else
            q = (s != 0 && s > 999) ? string.Format("{0:0,0}", s) : s.ToString();
        return q;
    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Noidung"] = txtnoidung.Text.Trim();

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                GV1.DataSource = dtCurrentTable;
                GV1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();

    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");

                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    txtnoidung.Focus();
                    txtnoidung.Text = dt.Rows[i]["Noidung"].ToString();



                    rowIndex++;
                }
            }
        }
    }



    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Noidung", typeof(string)));

        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Noidung"] = string.Empty;

        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;


        GV1.DataSource = dt;
        GV1.DataBind();
    }
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable"] = dt;
                GV1.DataSource = dt;
                GV1.DataBind();

                for (int i = 0; i < GV1.Rows.Count - 1; i++)
                {
                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }
    }
    private void SetRowData()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Noidung"] = txtnoidung.Text.Trim();

                    rowIndex++;
                }

                ViewState["CurrentTable"] = dtCurrentTable;
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }

    protected void dropNhanVien_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dropNhanVien.SelectedIndex > 0)
        {
            getDate();
            dropnhacungcap.Visible = false;
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@NhanVien", dropNhanVien.SelectedValue);
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[2].SqlDbType = SqlDbType.SmallDateTime;
            dropnoidungnhanvien.Items.Clear();
            dropnoidungnhanvien.DataSource = ex.GetData("sp_PhieuChiCongNo_NoiDungTheoNhanVien", p);
            dropnoidungnhanvien.DataBind();
            dropnoidungnhanvien.Items.Insert(0, new ListItem("--Chọn nội dung--", ""));
            dropnoidungnhanvien.SelectedIndex = 0;
            dropnoidungnhanvien.Visible = true;
        }
        else
        {
            dropnhacungcap.Visible = true;
            dropnoidungnhanvien.Visible = false;
        }
    }
    protected void dropnhacungcap_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dropnhacungcap.SelectedIndex > 0)
        {
            dropnoidungnhanvien.Visible = false;
            dropNhanVien.Visible = false;
            getDate();
            dropnhacungcap.Visible = true;
            dropnoidungnhacungcap.Visible = true;
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@NhaCungCap", dropnhacungcap.SelectedValue);
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[2].SqlDbType = SqlDbType.SmallDateTime;
            dropnoidungnhacungcap.Items.Clear();
            dropnoidungnhacungcap.DataSource = ex.GetData("sp_PhieuChiCongNo_NoiDungTheoNhaCungCap", p);
            dropnoidungnhacungcap.DataBind();
            dropnoidungnhacungcap.Items.Insert(0, new ListItem("--Chọn nội dung--", ""));
            dropnoidungnhacungcap.SelectedIndex = 0;
        }
        else
        {
            dropNhanVien.Visible = true;
            dropnoidungnhacungcap.Visible = false;
        }
    }
    protected void dropnoidungnhanvien_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[4];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@NoiDung", dropnoidungnhanvien.SelectedValue);
        p[2].SqlDbType = SqlDbType.NVarChar;

        p[3] = new SqlParameter("@NhanVien", dropNhanVien.SelectedValue);
        p[3].SqlDbType = SqlDbType.NVarChar;
        GVPhieuChi.DataSource = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NhanVien_NoiDung", p);
        GVPhieuChi.DataBind();
        LoadFooterGV();
    }
    protected void dropnoidungnhacungcap_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[4];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@NhaCungCap", dropnhacungcap.SelectedValue);
        p[2].SqlDbType = SqlDbType.NVarChar;
        p[3] = new SqlParameter("@NoiDung", dropnoidungnhacungcap.SelectedValue);
        p[3].SqlDbType = SqlDbType.NVarChar;
        GVPhieuChi.DataSource = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NCC_NoiDung", p);
        GVPhieuChi.DataBind();
        LoadFooterGV();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        getDate();
        if (dropNhanVien.SelectedIndex == 0 && dropnhacungcap.SelectedIndex == 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;

            GVPhieuChi.DataSource = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NV", p);
            GVPhieuChi.DataBind();
            LoadFooterGV();
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;

            dtPrint = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NV_Print", p);
        }
        else if (dropNhanVien.SelectedIndex > 0 && dropnhacungcap.SelectedIndex == 0)
        {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;


            p[2] = new SqlParameter("@NhanVien", dropNhanVien.SelectedValue);
            p[2].SqlDbType = SqlDbType.NVarChar;
            GVPhieuChi.DataSource = ex.GetData("sp_PhieuChiCongNo_TienMat_TheoThoiGian_NhanVien", p);
            GVPhieuChi.DataBind();
            LoadFooterGV();
            //btnFirst.Visible = false;
            //btnPre.Visible = false;
            //btnNext.Visible = false;
        }
    }

    void PrintNPOI(System.Data.DataTable dt)
    {
        var workbook = new HSSFWorkbook();

        if (dt.Rows.Count > 0)
        {
            string sheetname = "Danh sách";




            var sheet = workbook.CreateSheet(sheetname);

            #region CSS
            var headerLabelCellStyle = workbook.CreateCellStyle();
            headerLabelCellStyle.Alignment = HorizontalAlignment.LEFT;
            headerLabelCellStyle.BorderBottom = CellBorderType.THIN;
            headerLabelCellStyle.BorderLeft = CellBorderType.THIN;
            headerLabelCellStyle.BorderRight = CellBorderType.THIN;
            headerLabelCellStyle.BorderTop = CellBorderType.THIN;

            var headerLabelFont = workbook.CreateFont();
            headerLabelFont.FontName = "Times New Roman";

            headerLabelFont.FontHeightInPoints = 11;
            headerLabelFont.Color = HSSFColor.BLACK.index;
            headerLabelCellStyle.SetFont(headerLabelFont);

            //--------------------------

            var hsRight = workbook.CreateCellStyle();
            hsRight.Alignment = HorizontalAlignment.RIGHT;
            hsRight.BorderBottom = CellBorderType.THIN;
            hsRight.BorderLeft = CellBorderType.THIN;
            hsRight.BorderRight = CellBorderType.THIN;
            hsRight.BorderTop = CellBorderType.THIN;

            hsRight.SetFont(headerLabelFont);
            //-------------------
            var hsCenter = workbook.CreateCellStyle();
            hsCenter.Alignment = HorizontalAlignment.CENTER;
            hsCenter.BorderBottom = CellBorderType.THIN;
            hsCenter.BorderLeft = CellBorderType.THIN;
            hsCenter.BorderRight = CellBorderType.THIN;
            hsCenter.BorderTop = CellBorderType.THIN;

            hsCenter.SetFont(headerLabelFont);
            //-----------------------

            var hs1 = workbook.CreateCellStyle();
            hs1.Alignment = HorizontalAlignment.LEFT;

            var hsb = workbook.CreateFont();
            hsb.Boldweight = (short)FontBoldWeight.BOLD;
            hsb.FontName = "Times New Roman";

            hsb.FontHeightInPoints = 11;
            hsb.Color = HSSFColor.BLACK.index;
            hs1.SetFont(hsb);

            //-------------
            var hs2 = workbook.CreateCellStyle();
            hs2.Alignment = HorizontalAlignment.CENTER;

            var hsb2 = workbook.CreateFont();
            hsb2.Boldweight = (short)FontBoldWeight.BOLD;
            hsb2.FontName = "Times New Roman";

            hsb2.FontHeightInPoints = 18;
            hsb2.Color = HSSFColor.BLACK.index;
            hs2.SetFont(hsb2);
            //------------------------------
            var hs3 = workbook.CreateCellStyle();
            hs3.Alignment = HorizontalAlignment.CENTER;
            hs3.BorderBottom = CellBorderType.THIN;
            var hsb3 = workbook.CreateFont();
            hsb3.Boldweight = (short)FontBoldWeight.BOLD;
            hsb3.FontName = "Times New Roman";

            hsb3.FontHeightInPoints = 14;
            hsb3.Color = HSSFColor.BLACK.index;
            hs3.SetFont(hsb3);

            //----------------------------
            var hs4 = workbook.CreateCellStyle();
            hs4.Alignment = HorizontalAlignment.CENTER;

            hs4.BorderBottom = CellBorderType.THIN;
            hs4.BorderLeft = CellBorderType.THIN;
            hs4.BorderRight = CellBorderType.THIN;
            hs4.BorderTop = CellBorderType.THIN;

            var hsb4 = workbook.CreateFont();
            hsb4.Boldweight = (short)FontBoldWeight.BOLD;
            hsb4.FontName = "Times New Roman";

            hsb4.FontHeightInPoints = 11;
            hsb4.Color = HSSFColor.BLACK.index;
            hs4.SetFont(hsb4);

            #endregion

            //Start header-----------------------------------------------------------------------
            #region Header
            //tao hnag dau tien
            var rowIndex = 0;

            //cong ty
            var row = sheet.CreateRow(rowIndex);
            Cell r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Công ty cổ phần Xây Dựng Đức Anh");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 4);
            sheet.AddMergedRegion(cra);

            //chi nhanh
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Chi nhánh Trạm trộn Lâm Thao");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            cra = new CellRangeAddress(1, 1, 0, 4);
            sheet.AddMergedRegion(cra);

            //tieu de bao cao
            string ngaythang = dlThang.SelectedIndex > 0 ? dlThang.SelectedItem.Text + " " + dlNam.SelectedItem.Text : "Từ " + txtTuNgay.Text + " Đến " + txtDenNgay.Text;
            //  string congtrinh = dlCongTrinh.SelectedIndex > 0 ? " - " + dlCongTrinh.SelectedItem.Text : "";
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Danh sách phiếu chi tiền mặt" + ngaythang);
            r1c1.CellStyle = hs2;
            r1c1.Row.Height = 500;

            cra = new CellRangeAddress(2, 2, 0, 12);
            sheet.AddMergedRegion(cra);

            //khachhang
            //rowIndex++;
            //row = sheet.CreateRow(rowIndex);
            //r1c1 = row.CreateCell(0);
            //r1c1.SetCellValue(dlKhachHang.SelectedIndex > 0 ? "Khách hàng: " + dlKhachHang.SelectedItem.Text + congtrinh : dlLoaiKH.SelectedIndex > 0 ? "Loại khách hàng: " + dlLoaiKH.SelectedItem.Text : "");
            //r1c1.CellStyle = hs3;
            //r1c1.Row.Height = 500;



            //freeze panes

            #endregion
            //End header-----------------------------------------------------------------------

            #region Header1-2

            //header2
            rowIndex++;
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            string[] header = { "Ngày tháng", "Họ tên", "Số PC", "Nội dung", "Số tiền", "Số PTT", "Số PTthu","Số tiền còn lại"
                              };

            var cell = row.CreateCell(0);
            cell.SetCellValue("STT");
            cell.CellStyle = hs4;
            cell.Row.Height = 400;


            //tao tieu de cot
            for (int j = 0; j < header.Length; j++)
            {
                cell = row.CreateCell(j + 1);
                cell.SetCellValue(header[j].ToString());
                cell.CellStyle = hs4;
                cell.Row.Height = 500;

                sheet.AutoSizeColumn(j);

            }

            #endregion

            //ghi du lieu tung dong
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rowIndex++;
                row = sheet.CreateRow(rowIndex);
                cell = row.CreateCell(0);
                cell.SetCellValue((i + 1).ToString());
                cell.CellStyle = hsCenter;

                for (int j = 1; j <= dt.Columns.Count; j++)
                {
                    cell = row.CreateCell(j);

                    if (j == 5||j==8)
                    {
                        if (!dt.Rows[i][j - 1].ToString().Trim().Equals(""))
                        {
                            cell.SetCellType(CellType.NUMERIC);
                            cell.SetCellValue(double.Parse(dt.Rows[i][j - 1].ToString()));
                        }
                        else
                            cell.SetCellValue(dt.Rows[i][j - 1].ToString());
                        cell.CellStyle = hsRight;
                    }
                    else
                    {
                        cell.SetCellValue(dt.Rows[i][j - 1].ToString());

                        if (j <= 1)
                            cell.CellStyle = hsCenter;
                        else
                            cell.CellStyle = headerLabelCellStyle;
                    }
                    cell.Row.Height = 500;
                }

            }


            rowIndex++;
            row = sheet.CreateRow(rowIndex);

            cell = row.CreateCell(2);
            cell.SetCellValue("Tổng cộng");
            cell.Row.Height = 500;
            cell.CellStyle = hs4;

            string[] footer = { "F","I" };
            for (int k = 5; k < 6; k++)
            {

                cell = row.CreateCell(k);
                cell.CellFormula = "Sum(" + footer[k - 5] + "6:" + footer[k - 5] + rowIndex.ToString() + ")";
                cell.Row.Height = 500;
                cell.CellStyle = hs4;
            
            }
            for (int k = 8; k < 9; k++)
            {

                cell = row.CreateCell(k);
                cell.CellFormula = "Sum(" + footer[k - 7] + "6:" + footer[k -7] + rowIndex.ToString() + ")";
                cell.Row.Height = 500;
                cell.CellStyle = hs4;

            }

            //fix lai border
            r1c1 = row.CreateCell(0);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            r1c1 = row.CreateCell(6);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            r1c1 = row.CreateCell(1);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            r1c1 = row.CreateCell(3);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(4);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(7);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

         
            for (int ik = 0; ik <= 7; ik++)
            {
                sheet.AutoSizeColumn(ik);
            }


            //end sheet tong hop--------------------------
            using (var exportData = new MemoryStream())
            {
                workbook.Write(exportData);

                string saveAsFileName = "DSPhieuChi-" + " " + ngaythang + ".xls";

                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
                Response.Clear();
                Response.BinaryWrite(exportData.GetBuffer());
                Response.End();
            }
        }
    }
    void PrintNPOI1(System.Data.DataTable dt)
    {
        var workbook = new HSSFWorkbook();

        if (dt.Rows.Count > 0)
        {
            string sheetname = "Danh sách";




            var sheet = workbook.CreateSheet(sheetname);

            #region CSS
            var headerLabelCellStyle = workbook.CreateCellStyle();
            headerLabelCellStyle.Alignment = HorizontalAlignment.LEFT;
            headerLabelCellStyle.BorderBottom = CellBorderType.THIN;
            headerLabelCellStyle.BorderLeft = CellBorderType.THIN;
            headerLabelCellStyle.BorderRight = CellBorderType.THIN;
            headerLabelCellStyle.BorderTop = CellBorderType.THIN;

            var headerLabelFont = workbook.CreateFont();
            headerLabelFont.FontName = "Times New Roman";

            headerLabelFont.FontHeightInPoints = 11;
            headerLabelFont.Color = HSSFColor.BLACK.index;
            headerLabelCellStyle.SetFont(headerLabelFont);

            //--------------------------

            var hsRight = workbook.CreateCellStyle();
            hsRight.Alignment = HorizontalAlignment.RIGHT;
            hsRight.BorderBottom = CellBorderType.THIN;
            hsRight.BorderLeft = CellBorderType.THIN;
            hsRight.BorderRight = CellBorderType.THIN;
            hsRight.BorderTop = CellBorderType.THIN;

            hsRight.SetFont(headerLabelFont);
            //-------------------
            var hsCenter = workbook.CreateCellStyle();
            hsCenter.Alignment = HorizontalAlignment.CENTER;
            hsCenter.BorderBottom = CellBorderType.THIN;
            hsCenter.BorderLeft = CellBorderType.THIN;
            hsCenter.BorderRight = CellBorderType.THIN;
            hsCenter.BorderTop = CellBorderType.THIN;

            hsCenter.SetFont(headerLabelFont);
            //-----------------------

            var hs1 = workbook.CreateCellStyle();
            hs1.Alignment = HorizontalAlignment.LEFT;

            var hsb = workbook.CreateFont();
            hsb.Boldweight = (short)FontBoldWeight.BOLD;
            hsb.FontName = "Times New Roman";

            hsb.FontHeightInPoints = 11;
            hsb.Color = HSSFColor.BLACK.index;
            hs1.SetFont(hsb);

            //-------------
            var hs2 = workbook.CreateCellStyle();
            hs2.Alignment = HorizontalAlignment.CENTER;

            var hsb2 = workbook.CreateFont();
            hsb2.Boldweight = (short)FontBoldWeight.BOLD;
            hsb2.FontName = "Times New Roman";

            hsb2.FontHeightInPoints = 18;
            hsb2.Color = HSSFColor.BLACK.index;
            hs2.SetFont(hsb2);
            //------------------------------
            var hs3 = workbook.CreateCellStyle();
            hs3.Alignment = HorizontalAlignment.CENTER;
            hs3.BorderBottom = CellBorderType.THIN;
            var hsb3 = workbook.CreateFont();
            hsb3.Boldweight = (short)FontBoldWeight.BOLD;
            hsb3.FontName = "Times New Roman";

            hsb3.FontHeightInPoints = 14;
            hsb3.Color = HSSFColor.BLACK.index;
            hs3.SetFont(hsb3);

            //----------------------------
            var hs4 = workbook.CreateCellStyle();
            hs4.Alignment = HorizontalAlignment.CENTER;

            hs4.BorderBottom = CellBorderType.THIN;
            hs4.BorderLeft = CellBorderType.THIN;
            hs4.BorderRight = CellBorderType.THIN;
            hs4.BorderTop = CellBorderType.THIN;

            var hsb4 = workbook.CreateFont();
            hsb4.Boldweight = (short)FontBoldWeight.BOLD;
            hsb4.FontName = "Times New Roman";

            hsb4.FontHeightInPoints = 11;
            hsb4.Color = HSSFColor.BLACK.index;
            hs4.SetFont(hsb4);

            #endregion

            //Start header-----------------------------------------------------------------------
            #region Header
            //tao hnag dau tien
            var rowIndex = 0;

            //cong ty
            var row = sheet.CreateRow(rowIndex);
            Cell r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Công ty cổ phần Xây Dựng Đức Anh");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 4);
            sheet.AddMergedRegion(cra);

            //chi nhanh
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Chi nhánh Trạm trộn Lâm Thao");
            r1c1.CellStyle = hs1;
            r1c1.Row.Height = 400;

            cra = new CellRangeAddress(1, 1, 0, 4);
            sheet.AddMergedRegion(cra);

            //tieu de bao cao
            string ngaythang = dlThang.SelectedIndex > 0 ? dlThang.SelectedItem.Text + " " + dlNam.SelectedItem.Text : "Từ " + txtTuNgay.Text + " Đến " + txtDenNgay.Text;
            //  string congtrinh = dlCongTrinh.SelectedIndex > 0 ? " - " + dlCongTrinh.SelectedItem.Text : "";
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            r1c1 = row.CreateCell(0);
            r1c1.SetCellValue("Danh sách phiếu chi tiền mặt theo nhân viên " + ngaythang);
            r1c1.CellStyle = hs2;
            r1c1.Row.Height = 500;

            cra = new CellRangeAddress(2, 2, 0, 12);
            sheet.AddMergedRegion(cra);

            //khachhang
            //rowIndex++;
            //row = sheet.CreateRow(rowIndex);
            //r1c1 = row.CreateCell(0);
            //r1c1.SetCellValue(dlKhachHang.SelectedIndex > 0 ? "Khách hàng: " + dlKhachHang.SelectedItem.Text + congtrinh : dlLoaiKH.SelectedIndex > 0 ? "Loại khách hàng: " + dlLoaiKH.SelectedItem.Text : "");
            //r1c1.CellStyle = hs3;
            //r1c1.Row.Height = 500;



            //freeze panes

            #endregion
            //End header-----------------------------------------------------------------------

            #region Header1-2

            //header2
            rowIndex++;
            rowIndex++;
            row = sheet.CreateRow(rowIndex);
            string[] header = { "Ngày tháng", "Họ tên", "Số PC", "Nội dung", "Số tiền", "Số PTT", "Số PTthu","Số tiền còn lại"
                              };

            var cell = row.CreateCell(0);
            cell.SetCellValue("STT");
            cell.CellStyle = hs4;
            cell.Row.Height = 400;


            //tao tieu de cot
            for (int j = 0; j < header.Length; j++)
            {
                cell = row.CreateCell(j + 1);
                cell.SetCellValue(header[j].ToString());
                cell.CellStyle = hs4;
                cell.Row.Height = 500;

                sheet.AutoSizeColumn(j);

            }

            #endregion

            //ghi du lieu tung dong
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rowIndex++;
                row = sheet.CreateRow(rowIndex);
                cell = row.CreateCell(0);
                cell.SetCellValue((i + 1).ToString());
                cell.CellStyle = hsCenter;

                for (int j = 1; j <= dt.Columns.Count; j++)
                {
                    cell = row.CreateCell(j);

                    if (j == 5)
                    {
                        if (!dt.Rows[i][j - 1].ToString().Trim().Equals(""))
                        {
                            cell.SetCellType(CellType.NUMERIC);
                            cell.SetCellValue(double.Parse(dt.Rows[i][j - 1].ToString()));
                        }
                        else
                            cell.SetCellValue(dt.Rows[i][j - 1].ToString());
                        cell.CellStyle = hsRight;
                    }
                    else
                    {
                        cell.SetCellValue(dt.Rows[i][j - 1].ToString());

                        if (j <= 1)
                            cell.CellStyle = hsCenter;
                        else
                            cell.CellStyle = headerLabelCellStyle;
                    }
                    cell.Row.Height = 500;
                }

            }


            rowIndex++;
            row = sheet.CreateRow(rowIndex);

            cell = row.CreateCell(2);
            cell.SetCellValue("Tổng cộng");
            cell.Row.Height = 500;
            cell.CellStyle = hs4;

            string[] footer = { "F" };
            for (int k = 5; k < 6; k++)
            {

                cell = row.CreateCell(k);
                cell.CellFormula = "Sum(" + footer[k - 5] + "6:" + footer[k - 5] + rowIndex.ToString() + ")";
                cell.Row.Height = 500;
                cell.CellStyle = hs4;

            }
            //fix lai border
            r1c1 = row.CreateCell(0);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            r1c1 = row.CreateCell(6);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            r1c1 = row.CreateCell(1);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;
            r1c1 = row.CreateCell(3);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(4);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;

            r1c1 = row.CreateCell(7);
            r1c1.CellStyle = hs4;
            r1c1.Row.Height = 400;


            for (int ik = 0; ik <= 7; ik++)
            {
                sheet.AutoSizeColumn(ik);
            }


            //end sheet tong hop--------------------------
            using (var exportData = new MemoryStream())
            {
                workbook.Write(exportData);

                string saveAsFileName = "DSPhieuChiNhanVien-" + " " + ngaythang + ".xls";

                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
                Response.Clear();
                Response.BinaryWrite(exportData.GetBuffer());
                Response.End();
            }
        }
    }

    protected void Button9_Click(object sender, EventArgs e)
    {
        PrintNPOI(dtPrint);
    }

    public string ToString(decimal number)
    {
        string s = number.ToString("#");
        string[] so = new string[] { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
        string[] hang = new string[] { "", "nghìn", "triệu", "tỷ" };
        int i, j, donvi, chuc, tram;
        string str = " ";
        bool booAm = false;
        decimal decS = 0;
        //Tung addnew
        try
        {
            decS = Convert.ToDecimal(s.ToString());
        }
        catch
        {
            return "";
        }
        if (decS < 0)
        {
            decS = -decS;
            s = decS.ToString();
            booAm = true;
        }
        i = s.Length;
        if (i == 0)
            str = so[0] + str;
        else
        {
            j = 0;
            while (i > 0)
            {
                donvi = Convert.ToInt32(s.Substring(i - 1, 1));
                i--;
                if (i > 0)
                    chuc = Convert.ToInt32(s.Substring(i - 1, 1));
                else
                    chuc = -1;
                i--;
                if (i > 0)
                    tram = Convert.ToInt32(s.Substring(i - 1, 1));
                else
                    tram = -1;
                i--;
                if ((donvi > 0) || (chuc > 0) || (tram > 0) || (j == 3))
                    str = hang[j] + str;
                j++;
                if (j > 3) j = 1;
                if ((donvi == 1) && (chuc > 1))
                    str = "một " + str;
                else
                {
                    if ((donvi == 5) && (chuc > 0))
                        str = "lăm " + str;
                    else if (donvi > 0)
                        str = so[donvi] + " " + str;
                }
                if (chuc < 0)
                    break;
                else
                {
                    if ((chuc == 0) && (donvi > 0)) str = "lẻ " + str;
                    if (chuc == 1) str = "mười " + str;
                    if (chuc > 1) str = so[chuc] + " mươi " + str;
                }
                if (tram < 0) break;
                else
                {
                    if ((tram > 0) || (chuc > 0) || (donvi > 0)) str = so[tram] + " trăm " + str;
                }
                str = " " + str;
            }
        }
        if (booAm) str = "Âm " + str;
        return str + "đồng chẵn";
    }
    protected void ckthunganhang_CheckedChanged(object sender, EventArgs e)
    {
        if (ckthunganhang.Checked == true)
        {

            drtaikhoan.Visible = true;
        }
        else drtaikhoan.Visible = false;
    }
}